﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Press.MVC.Data;
using Press.MVC.Services;
using System.Web.Routing;

namespace Press.MVC.Web.Controllers
{
    /// <summary>
    /// BlogController - MVC Press controller for everything "Blog"
    /// </summary>
    public class BlogController : BaseController
    {
         #region Constructor
        IBlogService _blogService;

        /// <summary>
        /// BlogController
        /// </summary>
        /// <param name="blogService"></param>
        public BlogController(IBlogService blogService) 
        {
            _blogService = blogService;
        }

        #endregion  

        /// <summary>
        /// Index - Default view for Blog Controller
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string blogname) 
        {
            BlogData bdata = new BlogData();
            bdata.Blog = _blogService.GetBlog(blogname);
            //return View("ShowBlog", bdata);

            return View("ShowBlog", bdata);
        }

        /// <summary>
        /// PostCommentSave - 
        /// </summary>
        /// <returns>Redirect to Post the user is currently viewing - RedirectToAction</returns>
        public ActionResult PostCommentSave()
        {
            int postID = 0;                
            postID = Convert.ToInt32(Request.Form["postid"]);

            PostComment pc = new PostComment();
            pc.Author = Request.Form["author"];
            pc.PostID = postID;
            pc.Body = Request.Form["comment"];
            pc.CreatedOn = DateTime.Now;
            pc.Email = Request.Form["email"];
            pc.Title = this.ReadFromRequest("title");
            _blogService.SavePostComment(pc);

            return RedirectToAction("ShowPost", "Blog", new { postId = postID });
        }

        /// <summary>
        /// ShowBlog - Get the Show Blog View(Show Blog page)
        /// </summary>
        /// <param name="blogid">Blog Id for single Blog to show</param>
        /// <returns>Show Blog View</returns>
        public ActionResult ShowBlog(string blogname)
        {         
            BlogData bdata = new BlogData();
            bdata.Blog = _blogService.GetBlog(blogname);
            return View("ShowBlog", bdata);
        }

        /// <summary>
        /// ShowPost - Get the Show Post View(Show Post page)
        /// </summary>
        /// <param name="postid">Post Id for the Post to show</param>
        /// <returns>ShowPost View</returns>
        public ActionResult ShowPost(int postid)
        {
            BlogData bdata = new BlogData();
            bdata.Post = _blogService.GetPost(postid);
            
            return View("ShowPost", bdata);
        }

        /// <summary>
        /// BlogRoll - Temporary view(RenderAction) to show all Blogs during development. FUTURE. To be used as a View to show all Blogs in a Blog Roll if enabled in Blog Settings
        /// </summary>
        /// <returns>Blog roll View</returns>
        public ActionResult BlogRoll()
        {
            return View("_BlogRoll", _blogService.GetBlogs());
        }

        /// <summary>
        /// PostCategoryList - Show the Post Category List
        /// </summary>
        /// <param name="blogId">Blog Id for Categories to show</param>
        /// <returns></returns>
        public ActionResult PostCategoryList(int blogId)
        {
            return View("_PostCategoryList", _blogService.GetPostCategories(blogId));
        }

    }
}
